package tools.old.files;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.*;
import java.util.prefs.BackingStoreException;

import javax.swing.*;
import javax.swing.GroupLayout.Group;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

import account.model.Account;
import account.model.SubTeam;
import account.student.controller.StudentController;
//import account.student.model.AccountTableModel;
import account.student.view.StudentAccountView;
import prototyping.client.AbstractClient;
import prototyping.client.student.controller.HelpButtonController;

@SuppressWarnings("serial")
public class MainView extends JFrame implements Observer{

	private static MainView instance;
	private static AbstractClient abc;

	private JButton btnVerzenden = new JButton("Verzenden");

	private JPanel labelpanel = new JPanel();
	private JLabel status = new JLabel("STATUS");
	private JLabel naam = new JLabel("NAAM");

	private JPanel tabelaccounts = new JPanel();
	private JTable tabel = new JTable();

	private JMenuBar menuBar = new JMenuBar();
	private JMenu Start = new JMenu("Start");
	private JMenu Help = new JMenu("Vraag");
	private JMenu Over = new JMenu("Help");
	private JMenuItem StartItem1 = new JMenuItem("Item 1");
	private JMenuItem StartItem2 = new JMenuItem("Item 2");
	private JMenuItem StartItem3 = new JMenuItem("Item 3");
	private JMenuItem StartItem4 = new JMenuItem("Item 4");
	private JMenuItem HelpItem2 = new JMenuItem("Item 2");
	private JMenuItem HelpItem4 = new JMenuItem("Item 4");	
	private JMenuItem mntmMelding = new JMenuItem("Melding");
	private JTextField msgField = new JTextField();
	
	protected JTextArea chatpanel = new JTextArea();

	private String ChatNaam = SubTeam.getInstance().getAcc().getPersoon().getVoornaam();

	public MainView(){
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		SubTeam.getInstance().addObserver(this);
		FrameSetup();		
	}

	public static MainView getInstance(){
		if (instance == null) {
			instance = new MainView();
		}
		return instance;
	}

	public void FrameSetup(){
		//framesetup.
		setVisible(true);
		setSize(800, 600);
		getContentPane().setLayout(null);
		setJMenuBar(menuBar);		
		setTitle("Hallo " + ChatNaam);

		//menubar
		menuBar.setForeground(Color.RED);
		menuBar.setBackground(Color.LIGHT_GRAY);

		Start.setForeground(Color.BLUE);
		StartItem1.setText("Account");
		StartItem4.setText("Afmelden");

		menuBar.add(Start);

		Start.add(StartItem1);
		Start.add(StartItem4);

		//labelpanel setup.
		labelpanel.setLayout(null);
		labelpanel.setBackground(Color.WHITE);
		labelpanel.setSize(300,200);
		
		//#center of JFrame.
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		this.setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2);

		//naam label.
		naam.setFont(new Font("Lucida Grande", Font.BOLD, 18));
		naam.setText(SubTeam.getInstance().getAcc().getStudent().getVoornaam() + " " + SubTeam.getInstance().getAcc().getStudent().getAchternaam());

		Insets insets = labelpanel.getInsets();
		Dimension size = naam.getPreferredSize();
		naam.setBounds(50 + insets.left, 50 + insets.top,
				150 + size.width,  20 + size.height);

		//status label.
		status.setFont(new Font("Lucida Grande", Font.PLAIN, 14));
		status = new JLabel("geen status");
		status.setText(SubTeam.getInstance().getAcc().getStudent().getStatus());

		Insets insets2 = labelpanel.getInsets();
		Dimension size2 = status.getPreferredSize();
		status.setBounds(53 + insets2.left, 80 + insets2.top,
				150 + size2.width, 20 + size2.height);

		//#label panel.
		labelpanel.add(naam);
		labelpanel.add(status);

		//tabelaccounts setup
		tabelaccounts.setLayout(null);
		tabelaccounts.setBackground(Color.WHITE);
		tabelaccounts.setBounds(0,200,300,this.getHeight()-60-labelpanel.getHeight());

		//tabel users laden.


//		tabel = new JTable(new AccountTableModel());

		JScrollPane scroll = new JScrollPane(tabel);
		scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		scroll.setBounds(0, 0, tabelaccounts.getWidth(), tabelaccounts.getHeight());
		scroll.setVisible(true);
		tabel.setPreferredScrollableViewportSize(new Dimension(283, getHeight()));

		//tabel.setRowHeight(60);
		tabelaccounts.add(scroll);
		chatpanel.setEditable(false);

		//chatframe => hier komt in de iteratie 2/3 de chat in.
		//chat panel setup
		chatpanel.setBounds(300, 0 ,483, 509);
		chatpanel.setBackground(Color.WHITE);
		Border loweredetched = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
		TitledBorder chat = BorderFactory.createTitledBorder(
				loweredetched, "Chat");
		chat.setTitleJustification(TitledBorder.RIGHT);
		chatpanel.setBorder(chat);		



		msgField.setBounds(300, 509, 368, 31);
		msgField.setColumns(10);

		btnVerzenden.setBounds(666, 511, 117, 29);

		//alles toevoegen aan het frame.
		getContentPane().add(labelpanel);
		getContentPane().add(tabelaccounts);
		getContentPane().add(chatpanel);
		getContentPane().add(msgField);
		getContentPane().add(btnVerzenden);

		//tekst textfield naar textarea
		ActionListener sendListener = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String CRLF = "\r\n"; // newline
				String str = msgField.getText();
				if (str != null && str.trim().length() > 0)
					chatpanel.setText("<" + ChatNaam + "> " + msgField.getText() + CRLF);
				msgField.selectAll();
				msgField.requestFocus();
				msgField.setText("");
			}
		};
		msgField.addActionListener(sendListener);
		btnVerzenden.addActionListener(sendListener);

		//frame openen bij het klikken op eigen naam.
		naam.addMouseListener(new MouseAdapter()   {   
			public void mouseClicked(MouseEvent e)   
			{StudentAccountView.getInstance().setVisible(true);}   
		});

		//frame openen bij het klikken op status.
		status.addMouseListener(new MouseAdapter()   {   
			public void mouseClicked(MouseEvent e)   
			{StudentAccountView.getInstance().setVisible(true);}   
		});

		//Alle buttons linken aan controller
		StartItem1.addActionListener(new StudentController(StartItem1));
		StartItem1.setActionCommand("AccountOpties");
		StartItem4.addActionListener(new StudentController(StartItem4));
		StartItem4.setActionCommand("Afmelden");
		Help.setForeground(Color.BLUE);

		mntmMelding.addActionListener(new StudentController(mntmMelding));
		mntmMelding.setActionCommand("HelpDocent");

		menuBar.add(Help);

		Help.add(mntmMelding);

		Over.setForeground(Color.BLUE);
		HelpItem2.setText("Aan de slag");
		HelpItem4.setText("Voorwaarden");
		menuBar.add(Over);
		Over.add(HelpItem2);
		Over.add(HelpItem4);
		HelpItem2.addActionListener(new StudentController(HelpItem2));
		HelpItem2.setActionCommand("HelpOverzicht");
		HelpItem4.addActionListener(new StudentController(HelpItem4));
		HelpItem4.setActionCommand("Voorwaardes");
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		//status en naam updaten
		status.setText(((Account)arg1).getStudent().getStatus());
		naam.setText(((Account)arg1).getPersoon().getVoornaam() + " " + ((Account)arg1).getPersoon().getAchternaam());
	}

}
